package c.e.a.b.a.a;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.RemoteException;
import android.util.Log;
import b.i.b.f;
import c.e.a.b.b.c;
import c.e.a.b.b.d;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.GuardedBy;

@ParametersAreNonnullByDefault
public class a {
    @GuardedBy("this")
    public c.e.a.b.b.a a;
    @GuardedBy("this")
    public c.e.a.b.e.a.b b;
    @GuardedBy("this")
    public boolean c;
    public final Object d = new Object();
    @GuardedBy("mAutoDisconnectTaskLock")
    public b e;
    @GuardedBy("this")
    public final Context f;
    public final boolean g;
    public final long h;

    public static final class a {
        public final String a;
        public final boolean b;

        public a(String str, boolean z) {
            this.a = str;
            this.b = z;
        }

        public final String toString() {
            String str = this.a;
            boolean z = this.b;
            StringBuilder stringBuilder = new StringBuilder(String.valueOf(str).length() + 7);
            stringBuilder.append("{");
            stringBuilder.append(str);
            stringBuilder.append("}");
            stringBuilder.append(z);
            return stringBuilder.toString();
        }
    }

    public static class b extends Thread {
        public WeakReference<a> a;
        public long b;
        public CountDownLatch c = new CountDownLatch(1);
        public boolean d = false;

        public b(a aVar, long j) {
            this.a = new WeakReference(aVar);
            this.b = j;
            start();
        }

        public final void run() {
            a aVar;
            try {
                if (!this.c.await(this.b, TimeUnit.MILLISECONDS)) {
                    aVar = (a) this.a.get();
                    if (aVar != null) {
                        aVar.a();
                        this.d = true;
                    }
                }
            } catch (InterruptedException unused) {
                aVar = (a) this.a.get();
                if (aVar != null) {
                    aVar.a();
                    this.d = true;
                }
            }
        }
    }

    public a(Context context, long j, boolean z, boolean z2) {
        Objects.requireNonNull(context, "null reference");
        if (z) {
            Context applicationContext = context.getApplicationContext();
            if (applicationContext != null) {
                context = applicationContext;
            }
        }
        this.f = context;
        this.c = false;
        this.h = j;
        this.g = z2;
    }

    /* JADX WARNING: Removed duplicated region for block: B:15:0x002b A:{Catch:{ all -> 0x0030 }} */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x002a A:{Catch:{ all -> 0x0030 }} */
    public static c.e.a.b.a.a.a.a b(android.content.Context r13) throws java.io.IOException, java.lang.IllegalStateException, c.e.a.b.b.c, c.e.a.b.b.d {
        /*
        r0 = "Error while reading from SharedPreferences ";
        r1 = "GmscoreFlag";
        r2 = new c.e.a.b.a.a.c;
        r2.m1216init(r13);
        r3 = "gads:ad_id_app_context:enabled";
        r3 = r2.a(r3);
        r4 = "gads:ad_id_app_context:ping_ratio";
        r5 = 0;
        r6 = r2.a;	 Catch:{ all -> 0x001d }
        if (r6 != 0) goto L_0x0017;
    L_0x0016:
        goto L_0x0021;
    L_0x0017:
        r4 = r6.getFloat(r4, r5);	 Catch:{ all -> 0x001d }
        r12 = r4;
        goto L_0x0022;
    L_0x001d:
        r4 = move-exception;
        android.util.Log.w(r1, r0, r4);
    L_0x0021:
        r12 = 0;
    L_0x0022:
        r4 = "gads:ad_id_use_shared_preference:experiment_id";
        r5 = "";
        r6 = r2.a;	 Catch:{ all -> 0x0030 }
        if (r6 != 0) goto L_0x002b;
    L_0x002a:
        goto L_0x0034;
    L_0x002b:
        r0 = r6.getString(r4, r5);	 Catch:{ all -> 0x0030 }
        goto L_0x0035;
    L_0x0030:
        r4 = move-exception;
        android.util.Log.w(r1, r0, r4);
    L_0x0034:
        r0 = r5;
    L_0x0035:
        r1 = "gads:ad_id_use_persistent_service:enabled";
        r9 = r2.a(r1);
        r1 = new c.e.a.b.a.a.a;
        r6 = -1;
        r4 = r1;
        r5 = r13;
        r8 = r3;
        r4.m1214init(r5, r6, r8, r9);
        r4 = android.os.SystemClock.elapsedRealtime();	 Catch:{ all -> 0x0064 }
        r13 = 0;
        r1.g(r13);	 Catch:{ all -> 0x0064 }
        r13 = r1.c();	 Catch:{ all -> 0x0064 }
        r6 = android.os.SystemClock.elapsedRealtime();	 Catch:{ all -> 0x0064 }
        r8 = r6 - r4;
        r11 = 0;
        r4 = r1;
        r5 = r13;
        r6 = r3;
        r7 = r12;
        r10 = r0;
        r4.h(r5, r6, r7, r8, r10, r11);	 Catch:{ all -> 0x0064 }
        r1.a();
        return r13;
    L_0x0064:
        r13 = move-exception;
        r5 = 0;
        r8 = -1;
        r4 = r1;
        r6 = r3;
        r7 = r12;
        r10 = r0;
        r11 = r13;
        r4.h(r5, r6, r7, r8, r10, r11);	 Catch:{ all -> 0x0071 }
        throw r13;	 Catch:{ all -> 0x0071 }
    L_0x0071:
        r13 = move-exception;
        r1.a();
        throw r13;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.a.b.a.a.a.b(android.content.Context):c.e.a.b.a.a.a$a");
    }

    public static c.e.a.b.b.a d(Context context, boolean z) throws IOException, c, d {
        try {
            context.getPackageManager().getPackageInfo("com.android.vending", 0);
            int a = c.e.a.b.b.b.b.a(context, 12451000);
            if (a == 0 || a == 2) {
                String str = z ? "com.google.android.gms.ads.identifier.service.PERSISTENT_START" : "com.google.android.gms.ads.identifier.service.START";
                c.e.a.b.b.a aVar = new c.e.a.b.b.a();
                Intent intent = new Intent(str);
                intent.setPackage("com.google.android.gms");
                try {
                    c.e.a.b.b.i.a a2 = c.e.a.b.b.i.a.a();
                    Objects.requireNonNull(a2);
                    context.getClass().getName();
                    if (a2.b(context, intent, aVar, 1)) {
                        return aVar;
                    }
                    throw new IOException("Connection failure");
                } catch (Throwable th) {
                    IOException iOException = new IOException(th);
                }
            } else {
                throw new IOException("Google Play services not available");
            }
        } catch (NameNotFoundException unused) {
            throw new c(9);
        }
    }

    public static c.e.a.b.e.a.b e(c.e.a.b.b.a aVar) throws IOException {
        try {
            IBinder a = aVar.a(10000, TimeUnit.MILLISECONDS);
            int i = c.e.a.b.e.a.c.a;
            IInterface queryLocalInterface = a.queryLocalInterface("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
            return queryLocalInterface instanceof c.e.a.b.e.a.b ? (c.e.a.b.e.a.b) queryLocalInterface : new c.e.a.b.e.a.d(a);
        } catch (InterruptedException unused) {
            throw new IOException("Interrupted exception");
        } catch (Throwable th) {
            IOException iOException = new IOException(th);
        }
    }

    /* JADX WARNING: Missing block: B:18:0x0035, code skipped:
            return;
     */
    public final void a() {
        /*
        r3 = this;
        r0 = "Calling this from your main thread can lead to deadlock";
        b.i.b.f.P(r0);
        monitor-enter(r3);
        r0 = r3.f;	 Catch:{ all -> 0x0036 }
        if (r0 == 0) goto L_0x0034;
    L_0x000a:
        r0 = r3.a;	 Catch:{ all -> 0x0036 }
        if (r0 != 0) goto L_0x000f;
    L_0x000e:
        goto L_0x0034;
    L_0x000f:
        r0 = r3.c;	 Catch:{ all -> 0x0022 }
        if (r0 == 0) goto L_0x002a;
    L_0x0013:
        r0 = c.e.a.b.b.i.a.a();	 Catch:{ all -> 0x0022 }
        r1 = r3.f;	 Catch:{ all -> 0x0022 }
        r2 = r3.a;	 Catch:{ all -> 0x0022 }
        java.util.Objects.requireNonNull(r0);	 Catch:{ all -> 0x0022 }
        r1.unbindService(r2);	 Catch:{ all -> 0x0022 }
        goto L_0x002a;
    L_0x0022:
        r0 = move-exception;
        r1 = "AdvertisingIdClient";
        r2 = "AdvertisingIdClient unbindService failed.";
        android.util.Log.i(r1, r2, r0);	 Catch:{ all -> 0x0036 }
    L_0x002a:
        r0 = 0;
        r3.c = r0;	 Catch:{ all -> 0x0036 }
        r0 = 0;
        r3.b = r0;	 Catch:{ all -> 0x0036 }
        r3.a = r0;	 Catch:{ all -> 0x0036 }
        monitor-exit(r3);	 Catch:{ all -> 0x0036 }
        return;
    L_0x0034:
        monitor-exit(r3);	 Catch:{ all -> 0x0036 }
        return;
    L_0x0036:
        r0 = move-exception;
        monitor-exit(r3);	 Catch:{ all -> 0x0036 }
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.a.b.a.a.a.a():void");
    }

    public a c() throws IOException {
        a aVar;
        f.P("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            if (!this.c) {
                synchronized (this.d) {
                    b bVar = this.e;
                    if (bVar == null || !bVar.d) {
                        throw new IOException("AdvertisingIdClient is not connected.");
                    }
                }
                try {
                    g(false);
                    if (!this.c) {
                        throw new IOException("AdvertisingIdClient cannot reconnect.");
                    }
                } catch (RemoteException e) {
                    Log.i("AdvertisingIdClient", "GMS remote exception ", e);
                    throw new IOException("Remote exception");
                } catch (Exception e2) {
                    throw new IOException("AdvertisingIdClient cannot reconnect.", e2);
                }
            }
            Objects.requireNonNull(this.a, "null reference");
            Objects.requireNonNull(this.b, "null reference");
            aVar = new a(this.b.getId(), this.b.x0(true));
        }
        f();
        return aVar;
    }

    /* JADX WARNING: Removed duplicated region for block: B:11:0x0019  */
    /* JADX WARNING: Missing exception handler attribute for start block: B:8:0x0011 */
    /* JADX WARNING: Can't wrap try/catch for region: R(7:2|3|(3:5|6|7)|8|9|(1:11)|12) */
    public final void f() {
        /*
        r6 = this;
        r0 = r6.d;
        monitor-enter(r0);
        r1 = r6.e;	 Catch:{ all -> 0x0024 }
        if (r1 == 0) goto L_0x0011;
    L_0x0007:
        r1 = r1.c;	 Catch:{ all -> 0x0024 }
        r1.countDown();	 Catch:{ all -> 0x0024 }
        r1 = r6.e;	 Catch:{ InterruptedException -> 0x0011 }
        r1.join();	 Catch:{ InterruptedException -> 0x0011 }
    L_0x0011:
        r1 = r6.h;	 Catch:{ all -> 0x0024 }
        r3 = 0;
        r5 = (r1 > r3 ? 1 : (r1 == r3 ? 0 : -1));
        if (r5 <= 0) goto L_0x0022;
    L_0x0019:
        r1 = new c.e.a.b.a.a.a$b;	 Catch:{ all -> 0x0024 }
        r2 = r6.h;	 Catch:{ all -> 0x0024 }
        r1.m1213init(r6, r2);	 Catch:{ all -> 0x0024 }
        r6.e = r1;	 Catch:{ all -> 0x0024 }
    L_0x0022:
        monitor-exit(r0);	 Catch:{ all -> 0x0024 }
        return;
    L_0x0024:
        r1 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x0024 }
        throw r1;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.a.b.a.a.a.f():void");
    }

    public void finalize() throws Throwable {
        a();
        super.finalize();
    }

    public final void g(boolean z) throws IOException, IllegalStateException, c, d {
        f.P("Calling this from your main thread can lead to deadlock");
        synchronized (this) {
            if (this.c) {
                a();
            }
            c.e.a.b.b.a d = d(this.f, this.g);
            this.a = d;
            this.b = e(d);
            this.c = true;
            if (z) {
                f();
            }
        }
    }

    public final boolean h(a aVar, boolean z, float f, long j, String str, Throwable th) {
        if (Math.random() > ((double) f)) {
            return false;
        }
        HashMap hashMap = new HashMap();
        Object obj = "1";
        String str2 = "0";
        hashMap.put("app_context", z ? obj : str2);
        if (aVar != null) {
            if (!aVar.b) {
                obj = str2;
            }
            hashMap.put("limit_ad_tracking", obj);
        }
        if (aVar != null) {
            String str3 = aVar.a;
            if (str3 != null) {
                hashMap.put("ad_id_size", Integer.toString(str3.length()));
            }
        }
        if (th != null) {
            hashMap.put("error", th.getClass().getName());
        }
        if (!(str == null || str.isEmpty())) {
            hashMap.put("experiment_id", str);
        }
        hashMap.put("tag", "AdvertisingIdClient");
        hashMap.put("time_spent", Long.toString(j));
        new b(hashMap).start();
        return true;
    }
}
